function clearUI() {
	alert("clearUI");
	$("#board td").html("&nbsp;");
}
function displayWinner(message) {
	alert("displayWinner");
	$("#menu .message").html(message);

	$("#menu").show();
}
function hideWinner() {
	alert("hideWinner");
	$("#menu").hide();
}
function setCellClickFunction(func) {
	alert("setCellClickFunction");
	$("#board td").click(func);
}
function setRestartClickFunction(func) {
	alert("setRestartClickFunction");
	$("#menu").append("<div id='play_again'>Play Again</div>");

	// Button for playing again.
	$("#play_again").click(func);
}
function setCellText(id, turn) {
	alert("setCellText");
	var space = $("#" + id); // Board space table element
	space.html(turn);
}

/* Main Game Handling class */
var TicTacToe = {
	turn : "O", // Keeps a record of who's turn it is
	board : [ "", "", "", "", "", "", "", "", "", "" ], // Keeps a record of the TicTacToe Board
	win : false, // records who won if the game is over

	setupGame : function() {
		// Add on-click events to each of the boxes of the board
		setCellClickFunction(function(e) {
			TicTacToe.move(e.target.id);
		});

		setRestartClickFunction(function() {
			TicTacToe.restartGame();
		});

		clearUI();
	},

	/* Clears and starts a new game with a new board */
	restartGame : function() {
		// Hide the menu
		hideWinner();

		// Clear the board
		this.board = [ "", "", "", "", "", "", "", "", "", "" ];
		this.win = false;

		// Clear the UI
		clearUI();
	},

	/* Handles clicks spaces on the board */
	move : function(id) {
		var num = id.replace("ttt", ""); // # representing the space on the board

		// If no one's gone there, and the game isn't over, go there!
		if (!this.board[num] && !this.win) {
			setCellText(id, this.turn);

			this.board[num] = this.turn;
			this.nextTurn(); // End turn
		}
	},

	/* Iterate turn and check if anyone one yet */
	nextTurn : function() {
		this.turn = (this.turn == "O") ? "X" : "O";
		this.win = this.check4Win();
		if (this.win) {
			this.endGame();
		}
	},

	/* Display who won and options for new games */
	endGame : function() {
		var message = "";

		if (this.win == "Cat") {
			message = "Cats Game.";
		} else {
			message = this.win + " wins!";
		}

		displayWinner(message);
	},

	// If any of these patters of board spaces have all X's or all O's somebody won!
	winning_moves : [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [6, 4, 2]],

	/* Check for whether someone won the game of it was a Cat's game. */
	check4Win : function() {

		// Loop through all possible winning combinations
		for (k in this.winning_moves) {
			var pattern = this.winning_moves[k];
			var p = this.board[pattern[0]] + this.board[pattern[1]] + this.board[pattern[2]];
			if (p == "XXX") {
				return "X"; // X Won!
			}
			if (p == "OOO") {
				return "O"; // O Won!
			}
		}

		// Check if all spaces in the board are filled, then its a Cat's game
		var cnt = 0;
		for (s in this.board) {
			if (this.board[s]) {
				cnt += 1;
			}
		}

		if (cnt == 9) {
			return "Cat"; // Cat's game!
		}
	}
};

$(document).ready(function() {
	// Start a game!
	TicTacToe.setupGame();
});
